# backend/app/routers/dashboard.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from backend.app.database.session import get_db
from backend.app.services.dashboard_service import get_dashboard_stats, get_recent_activities, update_daily_statistics

router = APIRouter()

@router.get("/dashboard/stats")
async def get_stats(db: Session = Depends(get_db)):
    """获取仪表盘统计数据"""
    try:
        stats = get_dashboard_stats(db)
        return {"success": True, "data": stats}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/dashboard/activities")
async def get_activities(limit: int = 10, db: Session = Depends(get_db)):
    """获取最近活动"""
    try:
        activities = get_recent_activities(db, limit)
        return {"success": True, "data": activities}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.post("/dashboard/update-statistics")
async def update_statistics(db: Session = Depends(get_db)):
    """更新统计数据（可以通过定时任务调用）"""
    try:
        update_daily_statistics(db)
        return {"success": True, "message": "统计数据已更新"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))